/* 
 * File:   timer.h
 * Author: ganrui
 *
 * Created on May 11, 2020, 9:47 AM
 */

#ifndef TIMER_H
#define	TIMER_H

#ifdef	__cplusplus
extern "C" {
#endif


#include "protocal.h"
#include "hardware.h"


enum {	
	TIMER_TIGER_CUTOFF = 0,
	TIMER_VDD_DELAY,
	TIMER_LOAD_RECOVERY,
	TIMER_CHECK_BATTERY_LOST,
	TIMER_CHECK_BATTERY_RESISTANCE,
	TIMER_CHECK_AC_VOLTAGE,
	TIMER_CHECK_CHARGER_VOLTAGE,
	TIMER_CHARGER_OPEN_DELAY,
	TIMER_MAIN_OPEN_DELAY,
	TIMER_CHECK_BAT_FULL,
	TIMER_CHECK_SYS_STATE,
	USER_TIMER_NUM
};

extern uint16_t utimer[USER_TIMER_NUM]; 
    
#define IS_TIMER_END(t_num)         (utimer[(t_num)] == 1 ? 1 : 0)
#define CLEAR_TIMER(t_num)          (utimer[(t_num)] = 0)
#define IS_TIMER_STARTED(t_num)     (utimer[(t_num)] != 0 ? 1 : 0)
#define IS_TIMER_RUNNING(t_num)     (utimer[(t_num)] > 1 ? 1 : 0)
/**
 max timer can be set: 65535 x 10ms /1000ms = 655.53s
 */
#define SET_TIMER_10MS(t_num, n10ms)   (utimer[(t_num)] = (n10ms) + 1)   
#define SET_TIMER_MS(t_num, ms)      (SET_TIMER_10MS((t_num), (ms)/10))

void user_timer_work(void);


#ifdef	__cplusplus
}
#endif

#endif	/* TIMER_H */

